import { createSlice } from "@reduxjs/toolkit";
import UserApi from "../../api/user"

const userStore = createSlice({
  name: "user",
  // 数据状态
  initialState: {
    user: {
      createTime: "",
      id: "",
      updateTime: "",
      userAvatar: null,
      userName: null,
      userProfile: null,
      userRole: "",
    }
  },
  // 同步修改方法
  reducers: {
    setUser(state, action) {
      state.user = action.payload
    }
  }
})

// 解构出actionCreater
const { setUser } = userStore.actions

// 获取reducer函数
const userReducer = userStore.reducer

const fetchLogin = (loginForm) => {
  return async (dispatch) => {
    const res = await UserApi.login(loginForm)
    console.log(res);
    dispatch(setUser(res.data))
  }
}

export { setUser, fetchLogin }

export default userReducer